<?php
/**
  * Pownce interface class
  * March 19 2008 Raj Deut
  * http://idontquiteknow.com
  *
  *
  * Version 1.1
  * 2008/03/18 - (1.1) Updated to include method for new Verify Auth API call
  * 2008/03/15 - (1.0) Covers all current 2.0 API calls
  *
  * Basic Usage:
  *  // Initiate Pownce object
  *  $pownce = new Pownce("username", "password", "apikey");
  *
  *  // Send a message to public
  *  $pownce->postMessage("public", "just say hi to everyone");
  *  $pownce->execute();
  *
  *  // View public message list, but limit to 10 entries
  *  $pownce->getPublicNotes();
  *  $pownce->addOption("limit", "10");
  *  $pownce->execute();
  *
  *
  * Methods:
  * NOTES
  *  getPublicNotes([$format])
  *  getUserNotes($user [, $format ])
  *  getNote($note_id [, $format ])
  *  getNoteRecipients($note_id [, $format ])
  *
  *  USER
  *  getUserProfile($user [, $format])
  *  getRelationships($user, $relationship [, $format ])
  *
  *  AUTH
  *  verifyAuth([$format])
  *
  *  POSTING
  *  sendToList([$format])
  *  postMessage($note_to, $note_body [, $format ])
  *  postLink($note_to, $note_body, $url [, $format ])
  *  postEvent($note_to, $event_name, $event_location, $event_date [, $format ])
  *  postFile($note_to, $file [, $format])
  *  postReply($reply_to, $note_body [, $format ])
  *
  *  MODIFIER & EXECUTE
  *   addOption($option_name, $value);
  *   execute();
  *  
  *  DEBUG
  *  lastStatusCode()
  *  lastAPICall()
  */
  
  
class Pownce {
	private $api_url = "http://api.pownce.com/2.0/";
	private $credentials;
	private $app_key;
	private $http_status;
	private $last_api_call;

	private $api_call;
	private $internal_error;
	private $require_credentials = false;	
	private $http_post = false;
	private $options_nonauth = array("");
	private $options_auth = array("");

	
	function __construct($username, $password, $app_key) {
		$this->credentials = base64_encode($username.":".$password);
		$this->app_key = $app_key;
	}
	
	
	function getPublicNotes($format = "xml") {
		$this->options_nonauth = array("type","limit","page","since_id",
									"type" => array("messages", "links", "events"));
		$api_call = $this->api_url ."note_lists.". $format;
		$api_call .= "?app_key=". $this->app_key;
		$this->api_call = $api_call;
	}
	
	function getUserNotes($user, $format = "xml") {
		$this->options_nonauth = array("type","limit","page","since_id",
									"type" => array("messages", "links", "events"));
		$this->options_auth = array(	"filter","set",
								"filter" => array("notes","replies","sent","public","private","nonpublic"));	
		$api_call = $this->api_url ."note_lists/". $user .".". $format;
		$api_call .= "?app_key=". $this->app_key;
		$this->api_call = $api_call;
	}
	
	function getNote($note_id, $format = "xml") {
		$this->options_nonauth = array("show_replies","page",
									"show_replies" => array("true","false"));
		$api_call = $this->api_url ."notes/". $note_id .".". $format;
		$api_call .= "?app_key=". $this->app_key;
		$this->api_call = $api_call;
	}
		
	function getNoteRecipients($note_id, $format = "xml") {
		$this->options_nonauth = array("limit","page");
		$api_call = $this->api_url ."notes/". $note_id ."/recipients.". $format;
		$api_call .= "?app_key=". $this->app_key;
		$this->api_call = $api_call;
	}
	
	function getUserProfile($user, $format = "xml") {
		$api_call = $this->api_url ."users/". $user .".". $format;
		$api_call .= "?app_key=". $this->app_key;
		$this->api_call = $api_call;
	}
	
	function getRelationships($user, $relationship , $format = "xml") {
		$this->options_nonauth = array("limit","page");
		if($relationship != "friends" && $relationship != "fans" && $relationship != "fan_of") {
			$this->internal_error .= "Invalid relationship value (". $relationship .")<br/>";
		}
		$api_call = $this->api_url ."users/". $user ."/". $relationship .".". $format;			
		$api_call .= "?app_key=". $this->app_key;
		$this->api_call = $api_call;
	}
		
	function verifyAuth($format = "xml") {
		$this->require_credentials = true;
		$api_call = $this->api_url ."auth/verify.". $format;
		$api_call .= "?app_key=". $this->app_key;
		$this->api_call = $api_call;
	}
	
	function sendToList($format = "xml") {
		$this->require_credentials = true;
		$api_call = $this->api_url ."send/send_to.". $format;
		$api_call .= "?app_key=". $this->app_key;
		$this->api_call = $api_call;
	}
	
	function postMessage($note_to, $note_body, $format = "xml") {
		$this->require_credentials = true;
		$this->http_post = true;
		$api_call = $this->api_url ."send/message.". $format;
		$api_call .= "?app_key=". $this->app_key;
		$api_call .= "&note_to=". urlencode($note_to);
		$api_call .= "&note_body=". urlencode($note_body);		
		$this->api_call = $api_call;
	}
	
	function postLink($note_to, $note_body, $url, $format = "xml") {
		$this->options_auth = array("");	
		$this->require_credentials = true;
		$this->http_post = true;
		$api_call = $this->api_url ."send/link.". $format;
		$api_call .= "?app_key=". $this->app_key;
		$api_call .= "&note_to=". urlencode($note_to);	
		$api_call .= "&note_body=". urlencode($note_body);				
		$api_call .= "&url=". urlencode($url);
		$this->api_call = $api_call;	
	}

	function postEvent($note_to, $event_name, $event_location, $event_date, $format = "xml") {
		$this->options_auth = array("note_body");	
		$this->require_credentials = true;
		$this->http_post = true;
		$api_call = $this->api_url ."send/event.". $format;
		$api_call .= "?app_key=". $this->app_key;
		$api_call .= "&note_to=". urlencode($note_to);		
		$api_call .= "&event_name=". urlencode($event_name);
		$api_call .= "&event_location=". urlencode($event_location);
		$api_call .= "&event_date=". urlencode($event_date);		
		$this->api_call = $api_call;	
	}
	
	function postFile($note_to, $file, $pro = false, $format = "xml") {
		$this->options_auth = array("note_body");
		$this->require_credentials = true;
		$this->http_post = true;
		$api_call = $this->api_url ."send/file";
		$api_call .= ($pro) ? "_pro." : ".";
		$api_call .= $format;
		$api_call .= "?app_key=". $this->app_key;
		$api_call .= "&note_to=". urlencode($note_to);		
		$api_call .= "&media_file=@". $file;
		$this->api_call = $api_call;	
	}

	function postReply($reply_to, $note_body, $format = "xml") {
		$this->options_auth = array("stars","rsvp",
								"stars" => array(1,2,3,4,5), "rsvp"=>array(1,2,3,4,5,6));	
		$this->require_credentials = true;
		$this->http_post = true;
		$api_call = $this->api_url ."send/reply.". $format;
		$api_call .= "?app_key=". $this->app_key;
		$api_call .= "&reply_to=". $reply_to;
		$api_call .= "&note_body=". urlencode($note_body);
		$this->api_call = $api_call;	
	}
	
	function addOption($option_name, $value) {
		/* Check to make sure a function has been called to add a filter too */
		if($this->api_call == NULL) { 
			$this->internal_error .= "No function has been called to add options to<br/>"; 
		} else {		
 			/* Validate filter */
 			if(in_array($option_name, $this->options_nonauth)) {
 				$this->require_credentials = false;
 				$option_validation = $this->options_nonauth;
 			} else if(in_array($option_name, $this->options_auth)) {
 				$this->require_credentials = true;
 				$option_validation = $this->options_auth;
			} else {
				$this->internal_error .= "Invalid option (". $option_name .")<br/>";
			}
			
			if(count($option_validation[$option_name]) > 0) {
				if(!in_array($value, $option_validation[$option_name])) {
					$this->internal_error .= "Invalid value (". $value .") for option \"". $option_name ."\"<br/>";
				}
			}
			
			$this->api_call .= "&". $option_name ."=". urlencode($value);
		}
	}


	function execute() {
		if($this->internal_error) {
			$result = $this->internal_error . "API Call: ". $this->api_call;
			$this->http_status = "N/A";
		} else {
			$result = $this->apiCall($this->api_call, $this->require_credentials, $this->http_post);
		}
		
		// reset to defaults
		$this->last_api_call = $this->api_call;
		$this->api_call = NULL;
		$this->options_nonauth = array("");
		$this->options_auth = array("");
		$this->require_credentials = false;
		$this->http_post = false;
		$this->internal_error = NULL;
		return $result;
	}
	
	
	private function apiCall($api_url, $require_credentials = false, $http_post = false) {
		$curl_handle = curl_init();
		curl_setopt($curl_handle, CURLOPT_URL, $api_url);
		curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
		
		if ($require_credentials) {
			curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array("Authorization: Basic ". $this->credentials));
		}
		
		if ($http_post) {
			curl_setopt($curl_handle, CURLOPT_POST, true);
			$post_array = explode("&", (substr($api_url, (strpos($api_url, "?") + strlen("?app_key=". $this->app_key)+1), strlen($api_url))) );
			foreach($post_array as $pair) {
				$thisPair = explode("=", $pair);
				$postvars[$thisPair[0]] = urldecode($thisPair[1]);
			}
			curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $postvars );
		}
		
		$pownce_data = curl_exec($curl_handle);
		$this->http_status = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
		$this->last_api_call = $api_url;
		curl_close($curl_handle);
		return $pownce_data;	
	}
	
	
	
	/* Debugging & Information methods */
	function setPownceAPI($api_url) {
		$this->api_url = $api_url;
	}
	
	function getPownceAPI() {
		return $this->api_url;
	}

	function lastStatusCode() {
		return $this->http_status;
	}
	
	function lastAPICall() {
		return $this->last_api_call;
	}	
}
?>